function checkFile(self, file) {
    if (!file.name) {
        return false;
    }
    if ((file.size / 1024) > 4900 ) {
        return true;
    }
    var type = file.name.split('.').pop();
    var types = ["png", "gif", "jpg"];
    if (types.indexOf(type) < 0) {
        return false;
    }
    return true;
}

window.AUI.customCompoents.define("aui-file", class extends window.AUI.BaseComponent {
    constructor(name) {
        super(name);
        this.props = {
            value: {
                required: true
            }
        };

        this.methods = {
            init() {
                var self = this;
                var file_id =  window.AUI.Utils.uniqueSelector("fileinput_");
                var options = {
                    url: "",
                    dataType: 'text',
                    singleFileUploads: true,
                    paramName: "file",
                    sequentialUploads: true,
                    autoUpload: false,
                    formData: {}
                };


                if (window.fuckIE9) {
                    window.ssdsdsdsdsds = $("#" + self.fileinputid);
                    window.ssdsdsdsdsds.uploadify({
                        'auto'     : false,
                        'fileTypeDesc' : 'Image Files',
                        // 'fileTypeExts' : '*.gif; *.jpg; *.png',
                        'fileObjName' : 'file',
                        // 'fileSizeLimit' : '5MB',
                        'formData'     : {
                            'timestamp' : new Date().valueOf()
                        },
                        'swf'      : window.globalUploadifyPath,
                        'uploader': self.uploadPath,
                        width: self.$el.offsetWidth,
                        height: self.$el.offsetHeight - 15,
                        onSelect : function(file) {
                            var isOk = self.checkFile(file);
                            if (isOk) {
                                self.currentFiles = [{
                                    name: file.name
                                }];
                                window.ssdsdsdsdsds.uploadify('settings','uploader',self.uploadPath);
                                self.$emit("changefilesuccess", {
                                    context: self,
                                    file: file,
                                    upload() {
                                        window.ssdsdsdsdsds.uploadify("upload");
                                    }
                                });
                            } else {
                                self.$emit("changefileerror", file);
                            }

                            self.$emit("input", self.currentFiles[0].name);
                        },
                        onSelectError: function (file, errorCode, errorMsg) {
                            console.dir(errorCode);
                            // self.$emit("changefileerror", file);
                        },
                        onUploadError : function(file, errorCode, errorMsg, errorString) {
                            // alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
                            self.$emit("uploaderror", file, errorCode, errorString);
                        },
                        onUploadSuccess(file, data, response) {
                            console.log("upload succes");
                            self.$emit("uploadsuccess", file, data, response);
                        }
                    });

                } else {
                    var uploader = $(self.$refs.fileinput).fileupload(options);
                    uploader.on("fileuploadadd", function(evt, data) {
                        console.log("Add file");

                        data.url = self.uploadPath;

                        self.globalfilesubmit = data;
                        window.globalfilesubmit = data;

                        data.file_id = file_id;

                        self.handleFileChange(evt, data);

                        self.$emit("fileadd", evt, data);
                    });
                    uploader.on("fileuploadstart", function(evt, data) {
                        console.log("upload start");

                        self.$emit("uploadstart", evt, data);
                    });
                    uploader.on("fileuploaddone", function(evt, data) {
                        console.log("upload done");
                        var file = {};
                        self.$emit("uploadsuccess", data.files[0], data.result, data);
                    });
                    uploader.on("fileuploadfail", function(evt, data) {
                        console.log("upload failed");

                        self.$emit("uploaderror", data.files[0], data.textStatus, data.errorThrown);
                    });
                    uploader.on("fileuploadchange", function(evt, data) {
                        console.log("change");
                    });
                }
            },
            handleFileChange(e, data) {
                var self = this;
                if (data && data.files && data.files.length > 0) {
                    self.currentFiles = data.files;
                    var isok = self.checkFile(data.files[0]);
                    if (isok) {
                        self.$emit("changefilesuccess", {
                            context: self,
                            file: data.files[0],
                            upload() {
                                self.upload()
                            }
                        });
                        self.$emit("input", self.currentFiles[0].name);
                    }  else {
                        self.$emit("changefileerror", data.files[0]);
                    }
                }
            },
            checkFile(file){
                return checkFile(file)
            },
            setUploadPath(path) {
                var self = this;
                self.uploadPath = path;
            },
            checkFile(file) {
                return true;
            },
            upload() {
                var self = this;
                if (self.globalfilesubmit.submit) {
                    self.globalfilesubmit.submit();
                } else {
                    self.$emit("uploaderror", self.currentFiles[0],  "error", "");
                }
            }
        };

        this.beforeDestroy = function () {
            var self = this;
            self.$emit("savefiles", self.currentFiles);
        }
    }
    _template() {
        var self = this;
        return `<div class="${self._name}">
<div class="aui-file-upload">
   <input :id="fileinputid" name="file_upload" type="file" v-if="ie9fallbackshow">
  <div class="aui-file-select">                                                 
    <input v-if="!ie9fallbackshow" ref="fileinput" :id="fileinputid" type="file" /><div class="aui-file-select-name">
    <div v-if="currentFiles.length < 1">No file chosen...</div><div class="aui-file-select-name__file" v-if="currentFiles.length > 0">{{currentFiles[0].name}}</div>
</div><div class="aui-file-select-button">请选择文件</div>
  </div>  
</div> 
</div>`;
    }
    _data() {
        var ret = {};
        ret.currentFiles = [];
        ret.fileinputid = window.AUI.Utils.uniqueSelector("fileinput_");
        ret.ie9fallbackshow = false;
        ret.uploadPath = "";
        if (window.fuckIE9) {
            ret.ie9fallbackshow = true;
        }
        return ret;
    }
    _mounted() {
        var self = this;
        self.anuElement = $(self.$refs.fileinput);

        if (self.value) {
            self.currentFiles = [{
                name: self.value
            }];
        }

        self.$emit("beforeshow", self);

        var iframe = false;
        if(window.fuckIE9){
            iframe = true;
        }

        self.globalfilesubmit = {};


        if (!window.fuckIE9) {
            window.AUI.Loader.loadScript(
                "./jquery.fileupload.js"
            ).then(function () {
                self.init();
            });
        } else {
            self.init();
        }

        if (fuckIE9 || isIE10) {
            self.anuElement.mousedown(function() {
                $(this).trigger('click');
            })
        }
    }
});